package oocl.ita.eordering.service.impl;

import java.io.File;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import oocl.ita.eordering.dao.MerchantDao;
import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.service.MerchantService;

@Service("ms")
public class MerchantServiceImpl implements MerchantService {
	@Autowired
	private MerchantDao md;

	@Override
	@Transactional
	public int regist(Merchant merchant) {
		// TODO Auto-generated method stub
		return md.addMerchant(merchant);
	}

	@Override
	public Merchant login(String tel, String psd) {
		System.out.println("登录密码...."+psd);
		Merchant merchant=md.findMerchantByTel(tel);
		if (merchant==null) {
			return null;
		}
		String DBpsd=merchant.getmPassword();
		System.out.println("注册密码...."+DBpsd);
		if (psd.equals(DBpsd)) {
			return merchant;
		} else {
			System.out.println("登录失败");
			return null;
		}
		
	}

	@Override
	@Transactional
	public int updateMerchant(Merchant merchant) {
		// TODO Auto-generated method stub
		return md.updateMerchant(merchant);
	}


	@Override
	public boolean isTelExist(String mTel) {
		Merchant merchant=md.findMerchantByTel(mTel);
		if (merchant==null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	@Transactional
	public int updatePassword(Merchant merchant) {
		// TODO Auto-generated method stub
		return md.updatePassword(merchant);
	}

	@Override
	public String getIdCardPath(String mTel, MultipartFile cardPic) {
		File dir = null;
		String mCradPath = null;
		
		String fileName = cardPic.getOriginalFilename();
		if (fileName.lastIndexOf(".") == -1) {
			System.out.println("文件格式错误！！！");
		}
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		
		dir = new File("D:/MerchantUploadFiles/" + mTel);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		mCradPath = mTel + "/IDCard" + suffix;
		
		try {
			cardPic.transferTo(new File("D:/MerchantUploadFiles/" + mCradPath));
		} catch (IllegalStateException e) {
			System.out.println("IllegalStateException...文件保存本地错误");
			//return "errorPage";
		} catch (IOException e) {
			System.out.println("IOException...文件保存本地错误");
			//return "errorPage";
		}
		
		return mCradPath;
	}

	@Override
	public String getLogoPath(String mTel, MultipartFile logoPic) {
		File dir = null;
		String mLogoPath = null;
		
		String fileName = logoPic.getOriginalFilename();
		if (fileName.lastIndexOf(".") == -1) {
			System.out.println("文件格式错误！！！");
		}
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		
		dir = new File("D:/MerchantUploadFiles/" + mTel);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		mLogoPath = mTel + "/LOGO" + suffix;
		
		try {
			logoPic.transferTo(new File("D:/MerchantUploadFiles/" + mLogoPath));
		} catch (IllegalStateException e) {
			System.out.println("IllegalStateException...文件保存本地错误");
			//return "errorPage";
		} catch (IOException e) {
			System.out.println("IOException...文件保存本地错误");
			//return "errorPage";
		}
		
		return mLogoPath;
	}

	@Override
	public Merchant findMerchantById(Integer mId) {
		// TODO Auto-generated method stub
		return md.findMerchantById(mId);
	}

	@Override
	public Merchant findMerchantByTel(String mTel) {
		// TODO Auto-generated method stub
		return md.findMerchantByTel(mTel);
	}

}
